#ifndef DOCKWIDGET2_H
#define DOCKWIDGET2_H

#include <QWidget>


#include <QDockWidget>
#include <QTableWidget>
#include <QDateTime>
#include "overflawtimes.h"
namespace Ui {
class DockWidget2;
}
typedef struct  RecvBuf
{
    bool isShowed=false;
    int bufDataCount=0;
    ushort recvBuf[127];
}RecvBuf;

typedef struct OrignalDataUi
{
    int start;
    int end;
    int timeout;
    EveryRow row[128];
}OrignalDataUi;
class DockWidget2 : public QWidget
{
    Q_OBJECT

public:
    explicit DockWidget2(QWidget *parent = nullptr);
    ~DockWidget2();
    void SlotInit();
    void SetTableWigetByRow(int);
    void SetTableWidget1(int);
    void SetTableWidget2(int);
    void SetTableWidget3(int);
    void SetTableWidget4(int);
    void InitTableWidgetItem(int);
    void InitTableWidgetText(int);
    void UIInit();
    QString GetOriginalDataContent(QString data, int& value);
    template<typename T> void BoundsChecking(T,T,T,int);

    int type=0;                                         //type=1 03码 type=2 04码
    int currentID=0;                                    //在全局数组中的下标
    ushort registerStart=0;                             //起始寄存器
    ushort registerSum=0;                               //寄存器数目
    uint32_t sendFrameCount=0;                          //发送的帧数
    uint32_t recvFrameCount=0;                          //接收的帧数
    int timeout=0;                                      //轮询时间间隔
    OrignalDataUi dataUI;
    int count=0;                                        //存储文件序号
    int overflawFolderCount;                            //存储报警文件 文件夹序号

    OverFlawTimes overFlawTime;

    QTableWidget*tableWidget[4]={nullptr};
    QTableWidgetItem*GetItem(int index,int column);
    void UpdateSendFrame();
    void UpdateRecvFrame();
    void CreateFolder();
    void GetSaveFileName();
    void SaveFile(QByteArray send,QByteArray recv);
    void UpdateOverFlawTime(int i,QByteArray recvDataSave);
    void SaveOverFlawLog(int i,QByteArray recv,QDateTime dateTime);
    int GetWaringFolderCount();
    template<typename T> void DoShowEvents(T);

    void Handle16bitStype(int i,RecvBuf value,QByteArray recvData);
    void Handle16bitUtype(int i,RecvBuf value,QByteArray recvData);
    void Handle16bitXorBType(int i,RecvBuf value,QByteArray recvData);

    void Handle32bitSType(int i,RecvBuf value,QByteArray recvData,uint32_t combine);
    void Handle32bitUType(int i,RecvBuf value,QByteArray recvData,uint32_t combine);
    void Handle32bitXorBType(int i,RecvBuf value,QByteArray recvData,uint32_t combine);

    void Handle64bitSType(int i,RecvBuf value,QByteArray recvData,uint64_t combine);
    void Handle64bitUType(int i,RecvBuf value,QByteArray recvData,uint64_t combine);
    void Handle64bitXorBType(int i,RecvBuf value,QByteArray recvData,uint64_t combine);
protected:
    void closeEvent(QCloseEvent *event) override;
signals:
    void To_ShowRecv(RecvBuf,QByteArray,QByteArray,long);
signals:
    void CloseCommunitcation();
public slots:
    void SetTableWidget();
    void LoadFile();
    void ShowRecv(RecvBuf,QByteArray,QByteArray,long);
    void ShowOverFlawTimes(int,int);
    void ShowOverFlawTimes2(int,int);
    void ShowOverFlawTimes3(int,int);
    void ShowOverFlawTimes4(int,int);

private:
    Ui::DockWidget2 *ui;
};
typedef struct DockWidgetElement
{
    bool isInUse=false;
    DockWidget2* widgetPtr=nullptr;
}DockWidgetElement;

typedef struct  DockWidgets
{
    QVector <int> currentIDVector;//涉及新增与删除
    DockWidgetElement element[128];
}DockWidgets;

#endif // DOCKWIDGET2_H
